/**
 * 
 * purpose: include section logic...
 * @auth: Ning
 * date: 2009-9-29
 * 
 * */
package core.section
{
	import com.components.loader.I_Loader;
	import com.components.loader.JSONLoader;
	import com.components.loader.LoaderCallback;
	import com.components.loader.XMLLoader;
	import com.components.net.tracking.TrackingData;
	import com.components.net.tracking.TrackingServerManagement;
	import com.components.swfaddress.SWFAddress;
	import com.components.swfaddress.SWFAddressEvent;
	
	import core.ease.EaseFade;
	import core.ease.EaseFadeEvents;
	import core.error.Errors;
	import core.section.events.SectionEvents;
	import core.section.session.SessionManager;
	
	import debug.Log;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	[Event(name="data_init", type="core.section.events.SectionEvents")]
    [Event(name="init", type="core.section.events.SectionEvents")]
    [Event(name="on_display", type="core.section.events.SectionEvents")]
	[Event(name="on_exit", type="core.section.events.SectionEvents")]
	[Event(name="ready_for_exit", type="core.section.events.SectionEvents")]
	
	public class SectionController extends Controller
	{
		//START...
		
		
		//PROPERTY...
		
		public static const XML_TYPE:String = "xml";
		public static const JSON_TYPE:String = "json";
		
		protected var isExiting:Boolean;
		
		protected var arrDatas:Array;
		protected var initDataType:Array;
		
		protected var logReleated:Boolean;
		
		protected var urlParams:Array;
		
		protected var postParams:Object;
		
		protected var initData:Array;
		
		protected var isInit:Boolean;
		
		protected var deeplink:Boolean = true;
		
		protected var tracking:Boolean = true;
		
		public var view:SectionView;
		
		public var model:SectionModel;
		
		public var pagename:String;
		
		public var pageURL:String;
		
		public var pageTitle:String;
		
		
		//CONSTRUCTOR...
		public function SectionController(target:DisplayObject)
		{
			
			super(target);
						
		}
		
		//METHODS...
								
		//config controller event listener...
		private function configEventListener():void
		{
			
			target.addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			
			target.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFrmStage);
														
			view.addEventListener(EaseFadeEvents.READY_FOR_EASE_IN,onViewReadyForEaseIn,false,1,true);	
			
			view.addEventListener(EaseFadeEvents.EASE_IN_OVER,onViewEaseInOver,false,1,true);
			
			view.addEventListener(EaseFadeEvents.READY_FOR_DISPLAY,onViewReadyForDisplay,false,1,true);
			
			view.addEventListener(EaseFadeEvents.READY_FOR_EASE_OUT,onViewReadyForEaseOut,false,1,true);
			
			view.addEventListener(EaseFadeEvents.EASE_OUT_OVER,exitHandler,false,1,true);
			
			if(deeplink)
			{
				
				SWFAddress.setStrict(true);
				
				SWFAddress.addEventListener( SWFAddressEvent.CHANGE,addressChgHandler,false,1,true);
				
				target.addEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
				
			}
			
			target.addEventListener(Event.REMOVED_FROM_STAGE,removeResizeHandler,false,0,true);
																		
		}
		
		
		//remove resize handler...
		private function removeResizeHandler(evt:Event):void
		{
			
			target.stage.removeEventListener(Event.RESIZE,onTargetResize);
			
		}
		
		//on target resize...
		private function onTargetResize(evt:Event):void
		{
			
			view.onResize();
			
		}
		
		
		//on add to stage...
		private function onAddToStageForResize(evt:Event):void
		{
			
			onTargetResize(null);
			
			target.stage.addEventListener(Event.RESIZE,onTargetResize);
			
		}
		
		
		//on removed...
		private function onRemoved(evt:Event):void
		{
			
			SWFAddress.removeEventListener( SWFAddressEvent.CHANGE,addressChgHandler);
						
		}
		
		//on swf address change...
		private function addressChgHandler(evt:SWFAddressEvent):void
		{
										
			var params:Array = DeepLink.getValue(pageURL);
			
			if(String(urlParams) != String(params) && params)
			{
				
				urlParams = params;
				
				if(isInit) onSwfAddressChange(params);
				
			} 	
			
		}
				
		// on target remove from stage...
		private function exitHandler(evt:Event):void
		{
									
			onExit();
			
			Log.log("SECTION STATUS: " + pagename + " " + pageTitle,"EXIT...");
			
			dispatchEvent( new SectionEvents(SectionEvents.ON_EXIT));
											
		}
		
		//on view ready for ease out...
		private function onViewReadyForEaseOut(evt:Event):void
		{
			
			isExiting = true;
			
			if(logReleated) SessionManager.unregister(this);
			
			onExiting();
			
			Log.log("SECTION STATUS: " + pagename + " " + pageTitle,"EXITING...");
			
			dispatchEvent(new SectionEvents(SectionEvents.READY_FOR_EXIT));
						
		}
				
		//on view ready for display...
		private function onViewReadyForDisplay(evt:EaseFadeEvents):void
		{
			
			if(isExiting) return;
															
			view.display();
			
			isInit = true;
								
			onDisplay();
						
			Log.log("SECTION STATUS: " + pagename + " " + pageTitle,"ON DISPLAY...");
			
			dispatchEvent(new SectionEvents(SectionEvents.ON_DISPLAY));		
			
		}
		
		//on view ease in over...
		private function onViewEaseInOver(evt:EaseFadeEvents):void
		{
			
			if(isExiting) return;
									
			view.phase = EaseFade.DISPLAY;
					
		}
		
		//on view ready for ease in...
		private function onViewReadyForEaseIn(evt:EaseFadeEvents):void
		{
			
			if(isExiting) return;
			
			onInit();
			Log.log("SECTION STATUS: " + pagename + " " + pageTitle,"ON INIT...");	
			dispatchEvent(new SectionEvents(SectionEvents.INIT));	
		}
				
		
		//load data...
		private function loadData():void
		{
				
			if(isExiting) return;
			
			//get post data...
			postParams = ParamsManager.getParams(pagename);
								
			//get url data...
			if(deeplink) urlParams = DeepLink.getValue(pageURL);
						
			//get init data...
			loadInitData();						
			
		}
		
		// load config data...
		protected function loadInitData():void
		{
			initData = new Array;
			
			if(arrDatas is Array && arrDatas.length > 0)
			{
				var loader:I_Loader;
				var callback:LoaderCallback;
				
				for(var i:uint = 0;i < arrDatas.length;i++)
				{	
					if(initDataType is Array && initDataType[i] != null)
					{
						var type:String = initDataType[i];
						
						if(type == SectionController.XML_TYPE)
							loader = new XMLLoader(arrDatas[i].toString());
						else if(type == SectionController.JSON_TYPE)
							loader = new JSONLoader(arrDatas[i].toString());
					}else{
						loader = new XMLLoader(arrDatas[i].toString());
					}
					
					callback = new LoaderCallback([initDataItemLoadComplete],[loadFail]);
					
					if(i == arrDatas.length - 1)
						callback.addSuccessCallback(initDataLoadComplete,0);
	
					model.load(loader,callback);
				}	
			}else{
				initDataLoadComplete();
			}
		}
		
		//save data item... every data items load complete will be executed...
		private function initDataItemLoadComplete(data:Object):void
		{
						
			initData.push(data);
			
		}
				
		//on load config file complete, load init data... on all init data load complete will execute...
		private function initDataLoadComplete(data:Object = null):void
		{
						
			onDataInit();
			
			Log.log("SECTION STATUS: " + pagename + " " + pageTitle,"ON DATA INIT...");			
						
			dispatchEvent( new SectionEvents(SectionEvents.DATA_INIT));
			
			view.phase = EaseFade.EASE_IN;
										
		}
		
		//other action: track section...
		protected function track():void
		{
			if(pageTitle && pageURL)
			{
				var objTrack:TrackingData = new TrackingData(pageURL,pageTitle);
				TrackingServerManagement.sendData(objTrack);
			}
		}	
							
		
		
		/** FOLLOWIN ARE ABSTACT METHODS, NEED TO BE OVERRID IN CLASS WHICH EXTENDED FORM THIS ***/
		
		//initialize...
		public function init():void
		{
			
			setInitVars();
			
			Errors.register(this);
			
			if(logReleated) SessionManager.register(this);
			
			if(deeplink) DeepLink.setFlag(pageURL,pageTitle);
						
			if(tracking) track();	
			
			if(target.stage)
			{
				
				onTargetResize(null);
				
				target.stage.addEventListener(Event.RESIZE,onTargetResize,false,0,true);
				
			}else{
								
				target.addEventListener(Event.ADDED_TO_STAGE,onAddToStageForResize,false,0,true);
												
			}							
						
			configEventListener();
			
			initController();
			
			/**load data**/
			
			loadData();		
			
		}
		
		
		//set init vars...
		protected function setInitVars():void
		{
						
			view = new SectionView(target);
			
			model = new SectionModel;
			
			pagename = "";
			
			pageURL = "";
			
			pageURL = "";
										
			
		}
		
		//init controller...
		protected function initController():void
		{
			
			// to do....
			
		}
						
		//on data init...
		protected function onDataInit():void
		{
			
			// TO DO...
			
		}
		
		//on init...
		protected function onInit():void
		{
			
			// TO DO...
			
		}
		
		//on display...
		protected function onDisplay():void
		{
			
			// TO DO...
			
		}
	
		
		//before exit...
		public function onExiting():void
		{
			
			// TO DO...
			
		}
			
		//on exit from stage...
		protected function onExit():void
		{
			
			// TO DO...
			
		}	
			
					
		//on load form external fail...
		protected function loadFail(evt:Event = null):void
		{
			
			dispatchEvent(evt);
			
		}
		
		protected function onSwfAddressChange(params:Array):void
		{
			
			// TO DO...
			
		}
		
		
			
		//on add to  stage...
		protected function onAddToStage(evt:Event):void
		{
			
			// TO DO...
						
		} 	
				
		//on remove from stage...
		protected function onRemoveFrmStage(evt:Event):void
		{
			
			// TO DO...
						
		} 	
				
		
		//on login...
		public function onLogin(userInfo:Object):void
		{
			
			// TO DO...
			
		}
		
		//on logout...
		public function onLogout():void
		{
		
			// TO DO...	
						
		}

		
		//END...
	}
}